<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');

require_once(TANGRA_MAIN_DIR.'exceptions/te_invalid_get_post_param.class.php');
require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');


require_once($WSC->get_site_inc_dir().'_sys/site_page_w_gform.class.php');


abstract class Site_Page_W_GFO extends Site_Page_W_GForm  {
	private $object_capture;
	protected $object_to_form_id_links;
	protected $object;

	function __construct($page_name, $redir_on_success_target_page, $object_capture) {
		parent::__construct($page_name, $redir_on_success_target_page);

		$this->object_capture = $object_capture;
	}


	public function set_object_capture($object_capture) {
		$this->object_capture = $object_capture;
	}


	public function get_object_capture() {
		return $this->object_capture;
	}


	public function init() {
		parent::init();

		$this->init_gfo_events();


		$v2 = new Site_Web_Page_View($this, 'not_exists');
		$this->add_view($v2);


		$svm = $this->get_svm();

		if (!$svm->is_page_var_registered($this->get_page_name(), 'object_to_form_id_links')) {
			$svm->register_page_var($this->get_page_name(), 'object_to_form_id_links');
			$svm->set_page_var($this->get_page_name(), 'object_to_form_id_links', new Vars_Manager());
		}

		$this->object_to_form_id_links = &$svm->get_page_var($this->get_page_name(), 'object_to_form_id_links');

		$this->object = $this->_create_object();
	}


	protected function init_gfo_events() {
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple('receiving_obj_id', Tangra_Parameter_Method::GET, $this->object_capture), 'receive_object_id');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple('add_new', Tangra_Parameter_Method::GET, 'add_new'), 'add_new_event');
	}


	public function default_event() {
		throw new Tangra_Module_Exception('Invalid event. "Add new" or object id expected');
	}


	protected function is_receiving_object_id() {
		$context = $this->get__context();

		return $context->exists_in_get($this->get_object_capture());
	}


	private function capture_object_id() {
		$context = $this->get__context();

		return $context->get_from_get($this->get_object_capture());
	}


	protected function add_new_event() {
		$this->add_self_call_get_param('add_new', 1);
		$this->on_add_new();
		$this->prepare_form();
		$this->object2form();
		return $this->mode_form();
	}


	protected function receive_object_id() {
		$object_id = $this->capture_object_id();
		$this->add_self_call_get_param($this->object_capture, $object_id);
		if (!tangra_is_int($object_id)) {
			throw new TE_Invalid_Get_Post_Param($this->capture_object_id().' = '.$object_id);
		}

		if ($this->load_object($object_id)) {
			$this->on_received_id($object_id);
			$this->prepare_form();
			$this->object2form();

			$ret = $this->mode_form();
		} else {
			$ret = $this->get_view('not_exists');
		}

		return $ret;
	}


	protected function load_object($obj_id) {
		$dbc = $this->get_dbc();
		$ret = $this->object->load_by_id($dbc, $obj_id);
		//TODO what when not found
		$dbc->disconnect();

		return $ret;
	}


	protected function save_object() {
		$dbc = $this->get_dbc();
		$dbc->start_trans();

		try {
			$id = $this->object->save($dbc);
		} catch (Tangra_Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			$dbc->disconnect();
			throw $e;
		}

		$dbc->complete_trans();
		$dbc->disconnect();

		return $id;
	}




	protected function on_accepted_submit() {
		$form_id = $this->form->get_field_value('form_id');

		if ($this->object_to_form_id_links->is_var_registered($form_id)) {
			$this->object = $this->object_to_form_id_links->get_var($form_id);
		} else {
			throw new Tangra_Module_Exception('form_id not found in object_to_form_id_links.');
		}
	}


	protected function on_process_submit() {
		$this->form2object();
		$this->save_object();
		$this->after_save();
	}


	private function _create_object() {
		$obj = $this->create_object();
		if (!($obj instanceof I_DB_Storable)) {
			throw new Tangra_Module_Exception('I_DB_Storable instance expected');
		}

		return $obj;
	}


	protected function on_mode_form() {
		$form_id = $this->form->get_field_value('form_id');
		$this->object_to_form_id_links->register_var($form_id);
		$this->object_to_form_id_links->set_var($form_id, $this->object);
	}


	protected function handle_submit() {
		$this->prepare_form();

		return parent::handle_submit();
	}


	abstract protected function prepare_form();

	abstract protected function create_object();

	abstract protected function object2form();

	abstract protected function form2object();

	abstract protected function after_save();

	protected function on_received_id($id) {
	}

	protected function on_add_new() {
	}

// inherited from dpage_w_gform
//	abstract protected function create_form();
//
//	abstract protected function prepare_form_view(Guardable_Form $form, TPLE_Exports &$exports);
//
//	abstract protected function on_good_submit();
//
//	abstract protected function on_bad_submit();

}



